package com.forgenet.rpc;

import com.ecwid.consul.v1.health.model.HealthService;
import com.forgenet.gate.session.GatePlayerSessionManager;
import com.forgenet.message.PacketId;
import com.forgenet.message.base.resp.RespCreateRolePacket;
import com.forgenet.message.rpc.RpcGateDispatchOtherRequest;
import com.forgenet.message.rpc.RpcOtherDispatchClientRequest;
import com.sparrow.common.util.JsonUtil;
import com.sparrow.rpc.future.RpcFuture;
import com.sparrow.servicemgr.consul.ConsulManager;
import com.sparrow.servicemgr.consul.domain.ConsistentServiceSelector;
import com.sparrow.socket.tcp.session.impl.GameSession;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class DispatchFacade implements IDispatchFacade {

    private static Logger logger = LoggerFactory.getLogger(DispatchFacade.class);

    @Autowired
    GatePlayerSessionManager sessionManager;
    @Autowired
    private ConsulManager consulManager;

    @Autowired
    private RedissonClient redisClient;
    @Override
    public RpcFuture<Void> dispatchToOtherReq(RpcGateDispatchOtherRequest request) {
        return null;
    }

    @Override
    public RpcFuture<Void> dispatchToClientReq(RpcOtherDispatchClientRequest request) {
        int msgId = request.getMsgId();
        Object msg = request.getMsg();
        logger.info("转发请求到客户端 msg:{}", JsonUtil.toJSON(msg));
        switch (msgId) {
            case PacketId.Game.RESP_CREATE_ROLE_PACKET: {
                handleCreateResp(request);
                break;
            }
            default: {
                handleCommonResp(request);
                break;
            }
        }
        return null;
    }

    private void handleCommonResp(RpcOtherDispatchClientRequest request) {
        GameSession session = sessionManager.getSession(request.getPlayerId());
        if (session == null) {
            return;
        }
        session.writeAndFlush(request.getMsg());
    }

    private void handleCreateResp(RpcOtherDispatchClientRequest request) {
        Object msg = request.getMsg();
        RespCreateRolePacket respCreateRolePacket = (RespCreateRolePacket) msg;
        GameSession session = sessionManager.getUnAuthSession(respCreateRolePacket.getAccount());
        if (session == null) {
            return;
        }

        sessionManager.bind(session, respCreateRolePacket.getPlayerId());
        session.setIdentity(respCreateRolePacket.getPlayerId());
        session.writeAndFlush(msg);

        //redis设置游戏服id
        ConsistentServiceSelector gameSelector = new ConsistentServiceSelector(consulManager, "game", 100);
        HealthService gameService = gameSelector.selectService("account-" + respCreateRolePacket.getAccount());
        String gameId = gameService.getService().getId();
        System.out.println("gate redis绑定游戏服id:" + gameId);
        RBucket<String> playerIdToGameData = redisClient.getBucket("player:route:" + respCreateRolePacket.getPlayerId());
        playerIdToGameData.set(gameId);
    }
}
